using System;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

namespace Voodoo.Sauce.Core
{
	[Serializable]
	public class ProductDescriptor
	{
		[Tooltip("The product identifier, as entered in stores. Usually of the form com.companyname.iap.productname")]
		[SerializeField]
		private string _productId;

		[SerializeField]
		private VoodooSettingsProductType _type;

		public string ProductId => _productId;

		public string OriginalProductId => _productId;

		public VoodooSettingsProductType Type => _type;

		public ProductDescriptor(string productId, VoodooSettingsProductType type)
		{
			_productId = productId;
			_type = type;
		}

		public bool IsValidAndroidProductId()
		{
			return IsValidAndroidProductId(_productId);
		}

		internal static bool IsValidAndroidProductId(string productId)
		{
			return new Regex("^[a-z0-9_.]*$", RegexOptions.Compiled).IsMatch(productId);
		}

		public string GetAndroidProductId()
		{
			return ParseAndroidProductId(_productId);
		}

		public static string ParseAndroidProductId(string productIdString)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < productIdString.Length; i++)
			{
				char c = productIdString[i];
				if (char.IsLetterOrDigit(c))
				{
					c = char.ToLower(c);
				}
				else if (i == 0 || (c != '_' && c != '.'))
				{
					c = '0';
				}
				stringBuilder.Append(c);
			}
			return stringBuilder.ToString();
		}
	}
}
